import request from "@/request";
import { httpForm } from "@/request/http";
import axios from "axios";
import { compress } from 'image-conversion';
import { BASE_URL } from "@/config";
import { signatureGenerate } from "@/utils/signatureUtil"
import i18n from "@/i18n";

// 图片上传
export const _uploadImage = (file, callback) => {
    const { timestamp, signature, systemRandom } = signatureGenerate()
    let comp = 0.6
    if (file.file.size > 512 * 1024) {
        comp = 0.2
    }
    // Toast.loading()
    return new Promise((resolve, reject) => {
        compress(file.file, comp).then(res => {
            const formData = new FormData()
            formData.append('file', res)
            axios.post(`${BASE_URL}public/uploadimg!execute.action`,
                formData,
                {
                    timeout: 15000,
                    headers:
                        {
                            "Content-Type": "multipart/form-data",
                            'tissuePaper': timestamp,
                            'sign': signature,
                            'systemRandom': systemRandom
                        },
                    onDownloadProgress: (progressEvent) => {
                        if (progressEvent.lengthComputeable) {
                            callback(((progressEvent.loaded / progressEvent.total) * 100).toFixed(2))
                        }
                    }
                }).then(res => {
                // Toast.clear()
                const { code, data } = res.data
                if (code / 1 === 0) {
                    resolve(data)
                }else{
                    file.status = 'failed'
                    file.message = i18n.t('图片上传失败');
                }
            }).catch(err => {
                // Toast.clear()
                reject(err)
            })
        })
    })
}

// 申请认证
export const _applyIdentify = (params) => {
    return httpForm({
        url: "/api/kyc!apply.action",
        method: "post",
        isLoading: true
    }, params)
};

// 认证信息
export const _getIdentify = () => {
    return request({
        url: '/api/kyc!get.action',
        method: 'post'
    })
}

// =================   闪兑 ================
export const _getWallet = () => {
    return httpForm({
        url: "/api/wallet!get.action",
        method: "post",
        isLoading: false
    })
};

//闪兑
export const _getAllWallet = () => {
    return request({
        url: '/api/wallet!getAll.action',
        loading: false,
        method: 'post'
    })
}
// 初始化接口
export const _initExchange = () => {
    return request({
        url: "/api/exchangeapplyorder!view.action",
        method: "post",
        isLoading: false
    })
};

// 闪兑
export const _exchange = (params) => {
    return request({
        url: '/api/exchangeapplyorder!buy_and_sell.action',
        method: 'post',
        loading: true,
        data: {
            volume: params.volume, // 数量
            symbol: params.symbol.toLowerCase(), // 闪兑币
            symbol_to: params.symbol_to.toLowerCase(), // 闪兑后的币
            session_token: params.session_token //
        }
    })
}

// 汇率
export const _exchangeRage = (params) => {
    return request({
        url: '/api/exchangeapplyorder!buy_and_sell_fee.action',
        method: 'post',
        data: {
            symbol: params.symbol.toLowerCase(), // 闪兑币
            symbol_to: params.symbol_to.toLowerCase(), // 闪兑后的币
            volume: params.volume || 1 // 闪兑数量
        }
    })
}

// 闪兑历史
export const _exchangeHistory = page_no => {
    return request({
        url: '/api/exchangeapplyorder!list.action',
        method: 'post',
        data: {
            page_no
        }
    })
}

// 获取质押余额
export const _getBalance = () => {
    return request({
        url: '/api/dapp!getbalance.action',
        method: "post"
    })
}

// 提现初始化参数
export const _initWidthdrawl = (eth) => {
    return request({
        url: '/api/dapp!exchange_fee.action',
        method: "post",
        data: {
            eth
        }
    })
}

// 提现申请
export const _widthdrawl = (eth) => {
    return request({
        url: '/api/dapp!exchange.action',
        method: "post",
        loading: true,
        data: {
            eth
        }
    })
}

// 交易记录
export const _records = (action = 'exchange', page_no = 1) => { // transfer
    return request({
        url: '/api/dapp!logs.action',
        method: "post",
        loading: false,
        data: {
            action,
            page_no
        }
    })
}


// 充值详情
export const _rechargeDetail = (order_no) => {
    return request({
        url: "api/rechargeBlockchain!get.action",
        method: "post",
        data: {
            order_no
        }
    })
}

// 提现详情
export const _withdrawDetail = (order_no) => {
    return request({
        url: "api/withdraw!get.action",
        method: "post",
        data: {
            order_no
        }
    })
}

// 帐变记录
export const _fundRecord = (category = 'exchange', page_no = 1, contentType = '') => {
    return request({
        url: "api/moneylog!list.action",
        method: "post",
        data: {
            category,
            page_no,
            contentType
        }
    })
}
//助力贷配置信息
export const _getLoan = ()=>{
    return httpForm({
        url:"api/loan!getLoanParamList.action",
        method:'post',
        loading:false,
    })
}

//助力贷订单列表
export const _loanOrderList = ()=>{
    return httpForm({
        url:"api/loan!getOrders.action",
        method:'post',
        loading:true,
    })
}

//贷款提交
export const _loanApply = (params)=>{
    return httpForm({
        url:"api/loan!apply.action",
        method:'post',
        loading:true,
    },params)
}

// 助力贷订单详情
export const _loanOrderDetail = (params)=>{
    return httpForm({
        url:"api/loan!getOrderDetail.action",
        method:'post',
        loading:false,
    },params)
}